package com.cet.qz.unit01.chapter1_1;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2020-12-10 21:26
 **/
public class Qz1_1_22 {

    public static int rank(int key,int[] a){
        return rank(key,a,0,a.length-1,0);
    }

    public static int rank(int key,int[] a,int lo,int hi,int depth){
        for (int i = 0; i < depth; i++) {
            System.out.print("  ");
        }
        System.out.println(lo + " "+hi);
        if(lo>hi)
            return -1;
        int mid = lo+(hi -lo)/2;
        if(key<a[mid])
            return rank(key,a,lo,mid-1,++depth);
        else if(key > a[mid])
            return rank(key,a,mid+1,hi,++depth);
        else
            return mid;
    }

    public static void main(String[] args) {

        int[] a=new int[100];
        for (int i = 0; i < 100; i++) {
            a[i] = i+1;
        }
        int result = rank(6, a);
        System.out.println(result);

    }
}
